﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Tool;
@{
    ViewBag.Title = "ProductEdit";
    Layout = "~/Views/Shared/_Base.cshtml";
    Product product = ViewBag.product;
    var editType = ViewBag.editType;
}
@section head{
<script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")"></script>
<script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/ueditor/editor_config.js"></script>
<script type="text/javascript" charset="utf-8" src="../../Content/ueditor/editor_all_min.js"></script>
}
<script type="text/javascript">
    var sumeditor = null;
    var rsumeditor = null;
    $(function () {
        $("#btnSave").click(function () {
            var summaryval = sumeditor.getContent().replace(/<\/?.*?>/ig, ''); // 匹配有问题
            var rsumvalue = rsumeditor.getContent().replace(/<\/?.*?>/ig, '');
            $.ajax({
                url: "/Product/ProductEdit",
                data: "productId=" + @product.ID + "&productName=" + $("#txtProductName").val() + "&summary=" + summaryval + "&rateSummary=" + rsumvalue,
                type: "post",
                success: function (result) {
                    if (result == "OK") {
                        alert("保存成功");
                        location.href = decodeURIComponent("/Product/Index");
                    }
                    else {
                        alert("保存失败");
                    }
                }
            });
        });

        $("#btnCancel").click(function () {
            location.href = decodeURIComponent("/Product/Index");
        });

        $("#btnClose").click(function(){
            location.href = decodeURIComponent("/Product/Index");
        });
    });

</script>

<div style="overflow-x:hidder;overflow-y:auto;height:100%">
<form action="/" method="post" id="frmProduct" name="frmProduct" style="background-color: white">
<div id="form1" class="liger-form" style="padding-top: 10px;">
    <table cellpadding="0" cellspacing="0" class="l-table-edit">
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star">*</span>&nbsp;产品名称:
            </td>
            <td align="left" class="l-table-edit-td">
                @{
                if (editType == 1){
                    <input name="txtProductName" type="text" id="txtProductName" ltype="text" value="@product.ProductName" validate="{required:true,notnull:true,maxlength:10}"  nullText="不能为空且不超过10个字"/>
                }
                else if (editType == -1){
                    <span id="productName">@product.ProductName</span>
                }}
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star">*</span>&nbsp;产品描述:
            </td>
            <td align="left" class="l-table-edit-td">
                @{
                if (editType == 1){
                    <script name="txtSummary" id="txtSummary" type="text/plain">@product.Summary</script>
                    <script type="text/javascript">
                        window.UEDITOR_CONFIG.initialFrameWidth = 500;
                        window.UEDITOR_CONFIG.initialFrameHeight = 150;
                        sumeditor = UE.getEditor('txtSummary');
                    </script>
                }
                else if (editType == -1){
                    <p id="summary" style="width:70%; word-break:break-all; word-wrap:break-word;">@product.Summary</p>
                }}
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" style="width:80px;">
                <span class="l-star"></span>&nbsp;收益描述:
            </td>
            <td align="left" class="l-table-edit-td">
                @{
                if (editType == 1){
                    <script name="txtRateSummary" id="txtRateSummary" type="text/plain">@product.RateSummary</script>
                    <script type="text/javascript">
                        window.UEDITOR_CONFIG.initialFrameWidth = 500;
                        window.UEDITOR_CONFIG.initialFrameHeight = 150;
                        rsumeditor = UE.getEditor('txtRateSummary');
                    </script>
                }
                else if (editType == -1){
                    <p id="rateSummary" style="width:70%; word-break:break-all; word-wrap:break-word;">@product.RateSummary</p>
                }}
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="left" class="l-table-edit-td">  
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="left" class="l-table-edit-td"> 
            </td>
            <td align="left">
                @{
                if (editType == 1){
                    <input type="button" value="保存" id="btnSave" name="btnSave" class="l-button" />
                    <input type="button" value="取消" id="btnCancel" name="btnCancel" class="l-button" />
                }
                else if (editType == -1){
                    <input type="button" value="关闭" id="btnClose" name="btnClose" class="l-button" />
                }}
            </td>
        </tr>
    </table>
    <br />
</div>
</form>
</div>
<script type="text/javascript">
    $(function () {
        $.validator.addMethod($
                    "notnull",
                    function (value, element, regexp) {
                        if (!value) return true;
                        return !$(element).hasClass("l-text-field-null");
                    },
                    "不能为空"
            );

        $.metadata.setType("attr", "validate");
        var v = $("#frmProduct").validate({
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                //todo
            }
         });
         $("#frmProduct").ligerForm();
        
    });

</script>